<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="事件编号" prop="eventId">
        <el-input
          v-model="queryParams.eventId"
          placeholder="请输入事件编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="事件来源" prop="eventSource">
        <el-input
          v-model="queryParams.eventSource"
          placeholder="请输入事件来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="房源码" prop="roomNum">
        <el-input
          v-model="queryParams.roomNum"
          placeholder="请输入房源码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="房源名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入房源名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="反馈人" prop="feedbackPerson">
        <el-input
          v-model="queryParams.feedbackPerson"
          placeholder="请输入反馈人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="反馈单位" prop="feedbackUnit">
        <el-input
          v-model="queryParams.feedbackUnit"
          placeholder="请输入反馈单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理人" prop="processor">
        <el-input
          v-model="queryParams.processor"
          placeholder="请输入处理人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理部门" prop="procDept">
        <el-input
          v-model="queryParams.procDept"
          placeholder="请输入处理部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="处理单位" prop="proc-unit">
        <el-input
          v-model="queryParams.proc_unit"
          placeholder="请输入处理单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="处理方式" prop="procMethed">
        <el-input
          v-model="queryParams.procMethed"
          placeholder="请输入处理方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="截止时间" prop="deadline">
        <el-date-picker clearable
          v-model="queryParams.deadline"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择截止时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="处理时间" prop="processTime">
        <el-date-picker clearable
          v-model="queryParams.processTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择处理时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable
          v-model="queryParams.createdTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ptsj:event:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ptsj:event:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ptsj:event:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-upload
          class="upload-demo"
          action="#"
          :file-list="eventUploadList"
          :http-request="eventDataUpload"
          :show-file-list="false">
          <el-button plain icon="el-icon-upload2" size="mini" type="primary" :loading="isDataUpload">导入</el-button>
        </el-upload>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="getMb"
        >模板下载</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ptsj:event:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <!-- <el-table-column label="事件编号" align="center" prop="eventId" /> -->
      <el-table-column label="事件来源" align="center" prop="eventSource" />
      <!-- <el-table-column label="房源码" align="center" prop="roomNum" /> -->
      <el-table-column label="场所名称" align="center" prop="placeName" />
      <el-table-column label="房源名称" align="center" prop="roomName" />
      <!-- <el-table-column label="事件类型" align="center" prop="eventType" /> -->
      <el-table-column label="事件内容" align="center" width="300" prop="eventContent">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            width="500"
            trigger="hover"
            :content="scope.row.eventContent">
            <span class="wrap-line-2" slot="reference">{{ scope.row.eventContent }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="反馈人" align="center" prop="feedbackPerson" />
      <el-table-column label="反馈单位" align="center" prop="feedbackUnit" />
      <!-- <el-table-column label="处理人" align="center" prop="processor" /> -->
      <el-table-column label="处理部门" align="center" prop="procDept" />
      <!-- <el-table-column label="处理单位" align="center" prop="proc-unit" /> -->
      <!-- <el-table-column label="处理方式" align="center" prop="procMethed" /> -->
      <!-- <el-table-column label="处理意见" align="center" prop="procOpinions" /> -->
      <!-- <el-table-column label="截止时间" align="center" prop="deadline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="处理时间" align="center" prop="processTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.processTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="处理状态" align="center" prop="status" >
        <template slot-scope="scope">
          {{judgeZt(scope.row.status, deptName)}}
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建人" align="center" prop="createdBy" /> -->
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="seeDetails(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ptsj:event:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ptsj:event:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改事件信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="事件编号" prop="eventId">
          <el-input v-model="form.eventId" placeholder="请输入事件编号" />
        </el-form-item> -->
        <el-form-item label="场所名称" prop="placeName">
          <el-input v-model="form.placeName" placeholder="请输入场所名称" />
        </el-form-item>
        <el-form-item label="房源名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入房源名称" />
        </el-form-item>
        <el-form-item label="事件来源" prop="eventSource" >
          <el-input v-model="form.eventSource" placeholder="请输入事件来源" disabled="true" />
        </el-form-item>
        <el-form-item label="反馈人" prop="feedbackPerson">
          <el-input v-model="form.feedbackPerson" placeholder="请输入反馈人" />
        </el-form-item>
        <el-form-item label="反馈人联系方式" prop="feedbackPhone">
          <el-input v-model="form.feedbackPhone" placeholder="请输入反馈人联系方式" />
        </el-form-item>
        <!-- <el-form-item label="房源码" prop="roomNum">
          <el-input v-model="form.roomNum" placeholder="请输入房源码" />
        </el-form-item> -->
        
        <el-form-item label="事件内容" prop="eventContent">
          <el-input type="textarea" v-model="form.eventContent"></el-input>
        </el-form-item>
        
        <!-- <el-form-item label="反馈单位" prop="feedbackUnit">
          <el-input v-model="form.feedbackUnit" placeholder="请输入反馈单位" />
        </el-form-item> -->
        <!-- <el-form-item label="处理人" prop="processor">
          <el-input v-model="form.processor" placeholder="请输入处理人" />
        </el-form-item>
        <el-form-item label="处理部门" prop="procDept">
          <el-input v-model="form.procDept" placeholder="请输入处理部门" />
        </el-form-item>
        <el-form-item label="处理单位" prop="proc-unit">
          <el-input v-model="form.proc_unit" placeholder="请输入处理单位" />
        </el-form-item>
        <el-form-item label="处理方式" prop="procMethed">
          <el-input v-model="form.procMethed" placeholder="请输入处理方式" />
        </el-form-item>
        <el-form-item label="处理意见" prop="procOpinions">
          <el-input v-model="form.procOpinions" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="截止时间" prop="deadline">
          <el-date-picker clearable
            v-model="form.deadline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择截止时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理时间" prop="processTime">
          <el-date-picker clearable
            v-model="form.processTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable
            v-model="form.createdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEvent, getEvent, delEvent, addEvent, updateEvent, batchAddEvent } from "@/api/ptsj/event";
import { mapState } from 'vuex';

export default {
  name: "Event",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 事件信息表格数据
      eventList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eventId: null,
        eventSource: null,
        roomNum: null,
        roomName: null,
        eventType: null,
        eventContent: null,
        feedbackPerson: null,
        feedbackUnit: null,
        processor: null,
        procDept: null,
        proc_unit: null,
        procMethed: null,
        procOpinions: null,
        deadline: null,
        processTime: null,
        status: null,
        createdBy: null,
        createdTime: null,
      },
      // 表单参数
      form: {eventContent:null},
      // 表单校验
      rules: {
        eventContent: [
            { required: true, message: '请填写事件内容', trigger: 'blur' }
          ]
      },
      eventUploadList: [],
      isDataUpload: false
    };
  },
  computed: {
    ...mapState(['user']),
    deptName() {
      let { deptName } = this.user;
      return deptName
    }
  },
  created() {
    this.getList();
  },
  methods: {
    async eventDataUpload(params) {
      this.isDataUpload = true;
      let formData = new FormData();
      formData.append('file', params.file);
      await batchAddEvent(formData).then(res => {
        try {
          // console.log(res)
          if(res.code == 200) {
            this.isDataUpload = false;
            this.getList();
          }
        } catch(error) {
          this.isDataUpload = false;
          console.log(error)
        }
      })
    },
    getMb() {
      const attachmentUrl = '/static/files/event-import.xlsx';
      const link = document.createElement("a");
      link.href = attachmentUrl;
      link.download = "事件导入模板.xlsx";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },
    judgeZt(val, deptName) {
      if(val == '0') {
        return '处理完毕'
      } else {
        return '待处理'
      }
      /* if(deptName == '协会') {
        if(val == '1') {
          return '待处理'
        } else if(val == '2') {
          return '已流转'
        } else if(val == '3') {
          return '退回'
        } else if(val == '0') {
          return '处理完毕'
        }
      } else {
        if(val == '0') {
          return '处理完毕'
        } else {
          return '待处理'
        }
      } */
    },
    /** 查询事件信息列表 */
    getList() {
      this.loading = true;
      listEvent(this.queryParams).then(response => {
        this.eventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        eventId: null,
        eventSource: '综治办推送',
        roomNum: null,
        roomName: null,
        eventType: null,
        eventContent: null,
        feedbackPerson: null,
        feedbackUnit: null,
        processor: null,
        procDept: null,
        proc_unit: null,
        procMethed: null,
        procOpinions: null,
        deadline: null,
        processTime: null,
        status: null,
        delFlag: null,
        createdBy: null,
        createdTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加事件信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEvent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改事件信息";
      });
    },
    /* 查看详情 */
    seeDetails(row) {
      this.$router.push({path: '/event/detail', query: { id: row.id }});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEvent(this.form).then(response => { 
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEvent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除事件信息编号为"' + ids + '"的数据项？').then(function() {
        return delEvent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ptsj/event/export', {
        ...this.queryParams
      }, `event_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
.wrap-line-2 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%; /* 确保文本在容器内部正确省略 */
}
</style>
